package com.boot.security.server.controller;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URLDecoder;
import java.util.List;

import com.boot.security.server.utils.FileUtil;
import com.boot.security.server.utils.mffl.ReturnBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.boot.security.server.annotation.LogAnnotation;
import com.boot.security.server.dao.FileInfoDao;
import com.boot.security.server.dto.LayuiFile;
import com.boot.security.server.dto.LayuiFile.LayuiFileData;
import com.boot.security.server.model.FileInfo;
import com.boot.security.server.page.table.PageTableHandler;
import com.boot.security.server.page.table.PageTableHandler.CountHandler;
import com.boot.security.server.page.table.PageTableHandler.ListHandler;
import com.boot.security.server.page.table.PageTableRequest;
import com.boot.security.server.page.table.PageTableResponse;
import com.boot.security.server.service.FileService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import springfox.documentation.annotations.ApiIgnore;

import javax.imageio.ImageIO;

@ApiIgnore
@Api(tags = "文件")
@RestController
@RequestMapping("/files")
public class FileController {

    @Autowired
    private FileService fileService;
    @Autowired
    private FileInfoDao fileInfoDao;

    @LogAnnotation
    @PostMapping
    @ApiOperation(value = "文件上传")
    public FileInfo uploadFile(MultipartFile file) throws IOException {
        return fileService.save(file);
    }

    /**
     * layui富文本文件自定义上传
     *
     * @param file
     * @param domain
     * @return
     * @throws IOException
     */
    @LogAnnotation
    @PostMapping("/layui")
    @ApiOperation(value = "layui富文本文件自定义上传")
    public LayuiFile uploadLayuiFile(MultipartFile file, String domain) throws IOException {
        FileInfo fileInfo = fileService.save(file);

        String path = fileInfo.getPath();
        File imageFile = new File(path);
        //800*450
        Image image = ImageIO.read(imageFile);

        int st = path.indexOf(".");
        String suffix = path.substring(st);

        BufferedImage image800 = FileUtil.compressImageScadvert(image, 800, 450,suffix);

        String n800Path = path.substring(0, st) + "_med" + path.substring(st);
        ImageIO.write(image800, path.substring(st + 1), new File(n800Path));
        //400*400
        BufferedImage image400 = FileUtil.compressImageScadvert(image, 400, 400,suffix);
        int image400Width = image400.getWidth();
        if (image400Width > 400) {
            int xy = image400Width - 400;
            int xynum = xy / 2;
            image400 = image400.getSubimage(xynum, 0, 400, 400);
        }
        String n400Path = path.substring(0, st) + "_min" + path.substring(st);
        ImageIO.write(image400, path.substring(st + 1), new File(n400Path));


        LayuiFile layuiFile = new LayuiFile();
        layuiFile.setCode(0);
        LayuiFileData data = new LayuiFileData();
        layuiFile.setData(data);
        data.setSrc(domain + "/statics" + fileInfo.getUrl());
        data.setTitle(file.getOriginalFilename());

        return layuiFile;
    }


    @PostMapping("/scAdvert")
    @ApiOperation(value = "广告文件上传")
    public FileInfo uploadFileScAdvert(MultipartFile file) throws IOException {
        FileInfo fileInfo = fileService.save(file);
        String path = fileInfo.getPath();
        String url = fileInfo.getUrl();
        File imageFile = new File(URLDecoder.decode(path, "utf-8"));
        //800*450
        int st = path.indexOf(".");
        String suffix = path.substring(st);
        String n800Path = path.substring(0, st) + "_advert" + suffix;

        int st1 = url.indexOf(".");
        String suffix1 = url.substring(st1);
        String url1 = url.substring(0, st1) + "_advert" + suffix1;

        Image image = ImageIO.read(imageFile);
        BufferedImage image720 = FileUtil.compressImageScadvert(image, 690, 300, suffix);

        ImageIO.write(image720, path.substring(st + 1), new File(n800Path));
        fileInfo.setUrl(url1);
        return fileInfo;

    }

    @GetMapping
    @ApiOperation(value = "文件查询")
    @PreAuthorize("hasAuthority('sys:file:query')")
    public PageTableResponse listFiles(PageTableRequest request) {
        return new PageTableHandler(new CountHandler() {

            @Override
            public int count(PageTableRequest request) {
                return fileInfoDao.count(request.getParams());
            }
        }, new ListHandler() {

            @Override
            public List<FileInfo> list(PageTableRequest request) {
                List<FileInfo> list = fileInfoDao.list(request.getParams(), request.getOffset(), request.getLimit());
                return list;
            }
        }).handle(request);
    }

    @LogAnnotation
    @DeleteMapping("/{id}")
    @ApiOperation(value = "文件删除")
    @PreAuthorize("hasAuthority('sys:file:del')")
    public void delete(@PathVariable String id) {
        fileService.delete(id);
    }

}
